package com.chendx.store.goods.controller;

import com.alibaba.fastjson.JSON;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.goods.dto.SpecDto;
import com.chendx.store.api.goods.SpecControllerApi;
import com.chendx.store.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by chendx on 2022/4/19.
 */
@RestController
@RequestMapping("/api/goods/spec")
public class SpecController implements SpecControllerApi {

    @Autowired
    private SpecService specService;


    @Override
    @PostMapping("/loadData")
    public ResponseResult loadSpec(SpecDto dto) {
        return specService.loadSpec(dto);
    }

    @Override
    @GetMapping("/loadByCategory/{categoryId}")
    public ResponseResult loadByCategory(@PathVariable Integer categoryId) {
        return specService.loadByCategory(categoryId);
    }

    @Override
    @PostMapping("/add")
    public ResponseResult add(@RequestBody SpecDto dto) {
        return specService.add(dto);
    }

    @Override
    @GetMapping("/one")
    public ResponseResult one(Integer id) {
        return ResponseResult.okResult(specService.getById(id));
    }

    @Override
    @GetMapping("/ntlist")
    public ResponseResult findNoTemplateList() {
        return specService.findNoTemplateList();
    }

    @Override
    @GetMapping("/loadByCid/{categoryId}")
    public ResponseResult loadByCid(@PathVariable Integer categoryId) {
        return specService.loadByCid(categoryId);
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("{\"颜色\":\"金色\"}");
        list.add("{\"版本\":\"8G+256G\"}");
        list.add("{\"颜色\":\"红色\"}");
        list.add("{\"颜色\":\"绿色\"}");
        list.add("{\"版本\":\"2G+256G\"}");
        list.add("{\"版本\":\"4G+256G\"}");

        List<Map> mapList = new ArrayList<>();
        for (String mstr : list){
            Map map = JSON.parseObject(mstr, Map.class);
            mapList.add(map);
        }
        Set<String> keys = new HashSet<>();
        for (Map map : mapList){
            Set set = map.keySet();
            for (Object key : set){
                keys.add(String.valueOf(key));
            }
        }
        System.out.println(keys);
        Map<String,List<String>> mm = new HashMap<>();
        for (String key : keys){
            List<String> values = new ArrayList<>();
            for (Map map : mapList){
                if (!StringUtils.isEmpty(map.get(key))){
                    values.add(String.valueOf(map.get(key)));
                }
            }
            mm.put(key,values);
        }
    }

}
